﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using LogLib;
using IPCPages;
using ComLib.ComFun;

namespace FormPageConfigs.ChildPages
{
    public partial class PageReceiptTaking : PageConfigBase
    {
        private TakeReceiptOption _option = new TakeReceiptOption();
        public TakeReceiptOption Option
        {
            get { return _option; }
            set { _option = value; }
        }

        public PageReceiptTaking()
        {
            InitializeComponent();
        }

        public override bool LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }

            bool bRet = _option.Load();
            if (!_option.Load())
            {
                _error = _option.ErrorMessage;
                Log.WriteAsync(_error);
            }
            return bRet;
        }

        public override bool InitCtrls()
        {
            if (PrinterService.CreatePrinterService())
            {
                int selectedIndex = 0;
                List<string> printers = PrinterService.Printers;
                for (int i = 0; i < printers.Count; i++)
                {
                    comboBoxPrinterList.Items.Add(printers[i]);
                    if (printers[i].Equals(_option.Printer))
                    {
                        selectedIndex = i;
                    }
                }
                comboBoxPrinterList.SelectedIndex = selectedIndex;
            }
         
            textBoxDepartment.Text = _option.Department;          
            _bInited = true;
            return true;
        }

        public override bool UpdateData()
        {          
            if (string.IsNullOrEmpty(textBoxDepartment.Text))
            {
                MessageBox.Show("使用单位不能为空");
                return false;
            }
            if (comboBoxPrinterList.SelectedIndex >= 0)
            {
                _option.Printer = comboBoxPrinterList.SelectedItem.ToString();
            }
            _option.Department = textBoxDepartment.Text;
            _option.Save();
            return true;
        }

        private void PageReceiptTaking_Load(object sender, EventArgs e)
        {

        }
    }
}
